package com.jvyou.app.ds.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.domain.model.Result;
import com.jvyou.app.common.enums.HttpCodeEnum;
import com.jvyou.app.common.utils.value.ValueChecker;
import com.jvyou.app.ds.domain.dto.QuestionDto;
import com.jvyou.app.ds.domain.entity.Question;
import com.jvyou.app.ds.domain.vo.NextQuestionVo;
import com.jvyou.app.ds.service.QuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 驾校题目表(Question)表控制层
 *
 * @author 橘柚
 * @since 2025-04-20 20:55:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/question")
public class QuestionController {

    private final QuestionService questionService;

    @GetMapping("/list")
    @SaCheckPermission("ds:question:list")
    public Result<List<Question>> list(@ModelAttribute QuestionDto questionDto) {
        return Result.success(questionService.listQuestion(questionDto));
    }

    @GetMapping("/page")
    @SaCheckPermission("ds:question:page")
    public Result<PageResult<Question>> page(@ModelAttribute QuestionDto questionDto) {
        return Result.success(questionService.pageQuestion(questionDto));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @SaCheckPermission("ds:question:get")
    public Result<Question> selectOne(@PathVariable Long id) {
        return Result.success(questionService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param question 实体对象
     * @return 新增结果
     */
    @PostMapping
    @SaCheckPermission("ds:question:insert")
    public Result<Question> insert(@RequestBody Question question) {
        question.setSource("local");
        boolean success = questionService.save(question);
        return success ? Result.success(question) : Result.error(HttpCodeEnum.ADD_FAIL);
    }

    /**
     * 修改数据
     *
     * @param question 实体对象
     * @return 修改结果
     */
    @PutMapping("/updateById")
    @SaCheckPermission("ds:question:update")
    public Result<Boolean> updateById(@RequestBody Question question) {
        boolean success = questionService.updateById(question);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.UPDATE_FAIL);
    }


    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @SaCheckPermission("ds:question:delete")
    public Result<Boolean> delete(@PathVariable Long id) {
        boolean success = questionService.removeById(id);
        return success ? Result.success(true) : Result.error(HttpCodeEnum.DELETE_FAIL);
    }


    /**
     * 获取下一道题目
     *
     * @return 单条数据
     */
    @GetMapping("/getNextQuestion")
    @SaCheckPermission("ds:question:get")
    public Result<NextQuestionVo> getNextQuestion(@ModelAttribute QuestionDto questionDto) {
        ValueChecker.checkNotEmpty(questionDto.getLicenseType(), "驾照类型不能为空");
        ValueChecker.checkNotEmpty(questionDto.getSubject(), "科目不能为空");
        return Result.success(questionService.getNextQuestion(questionDto));
    }

    /**
     * 获取下一道题目
     *
     * @return 单条数据
     */
    @GetMapping("/getNextQuestionByType")
    @SaCheckPermission("ds:question:get")
    public Result<NextQuestionVo> getNextQuestionByType(@RequestParam Long typeId) {
        return Result.success(questionService.getNextQuestionByType(typeId));
    }

    /**
     * 获取下一道题目
     *
     * @return 单条数据
     */
    @GetMapping("/getNextAiQuestion")
    @SaCheckPermission("ds:question:get")
    public Result<NextQuestionVo> getNextAiQuestion() {
        return Result.success(questionService.getNextAiQuestion());
    }

    /**
     * 获取下一道题目
     *
     * @return 单条数据
     */
    @GetMapping("/getNextErrorQuestion")
    @SaCheckPermission("ds:question:get")
    public Result<NextQuestionVo> getNextErrorQuestion() {
        return Result.success(questionService.getNextErrorQuestion());
    }


    @GetMapping("/getCurrentAnswerRecord")
    @SaIgnore
    public Result<NextQuestionVo> getCurrentAnswerRecord(@ModelAttribute QuestionDto questionDto) {
        ValueChecker.checkNotEmpty(questionDto.getLicenseType(), "驾照类型不能为空");
        ValueChecker.checkNotEmpty(questionDto.getSubject(), "科目不能为空");
        return Result.success(questionService.getCurrentAnswerRecord(questionDto));
    }

}

